package com.yssoft.service.sales;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.yssoft.entity.inspection.TInspReportAtt;
import com.yssoft.entity.purchase.TPurchaseAtt;
import com.yssoft.entity.sales.TCustomer;
import com.yssoft.entity.sales.TCustomer2;
import com.yssoft.entity.sales.TCustomerArea2;
import com.yssoft.entity.sales.TCustomerAtt;
import com.yssoft.entity.sales.TCustomerAtt2;
import com.yssoft.entity.sales.TSalesOrder;
import com.yssoft.mapper.sales.TCustomerAreaMapper2;
import com.yssoft.mapper.sales.TCustomerAttMapper;
import com.yssoft.mapper.sales.TCustomerAttMapper2;
import com.yssoft.mapper.sales.TCustomerMapper;
import com.yssoft.mapper.sales.TCustomerMapper2;
import com.yssoft.mapper.sales.TSalesOrderMapper;
import com.yssoft.utils.AppUserHelper;
import com.yssoft.utils.AppUtil;
import com.yssoft.utils.UploadFile;
import com.yssoft.vo.TCustomerVo;


@Service
public class CustomerService {
	
	private static Logger logger=Logger.getLogger(CustomerService.class);
	
	@Resource
	private TCustomerMapper tCustomerMapper;
	@Resource
	private TCustomerAttMapper tCustomerAttMapper;
	@Resource
	private TCustomerMapper2 tCustomerMapper2;
	@Resource
	private TCustomerAreaMapper2 tCustomerAreaMapper2;
	@Resource
	private TCustomerAttMapper2 tCustomerAttMapper2;
	@Resource
	private TSalesOrderMapper tSalesOrderMapper;
	
	/**
	 * 客户详情查看
	 * @param customerId
	 * @return
	 */
	public Map<String, Object> queryCustomer(Integer customerId) {
		/*查询条件整理*/
		Map<String, Object> param = new HashMap<>();
		param.put("customerId",customerId);
		
		Map<String, Object> customer = tCustomerMapper2.queryCustomerById(param);
		return customer;
	}
	
	/**
	 * 删除客户
	 * @param customerId
	 */
	public String deleteCustomer(Integer customerId, HttpServletRequest request){
		String result = "0";
		TSalesOrder tSalesOrder = new TSalesOrder();
		tSalesOrder.settCustomerId(customerId);
		tSalesOrder.setDeleteFlag("0");
		tSalesOrder.settEId(AppUserHelper.getCurrentCompanyId(request));
		List<TSalesOrder> tSalesOrderList = tSalesOrderMapper.select(tSalesOrder);
		if(tSalesOrderList.size() > 0){
			result = "1";
		} else {
			TCustomer2 tCustomer2 = new TCustomer2();
			tCustomer2.setId(customerId);
			tCustomer2.setDeleteFlag("1");
			tCustomerMapper2.updateByPrimaryKeySelective(tCustomer2);
		}
		return result;
	}
	
	/**
	 * 客户列表
	 * @param currentPage
	 * @param pageSize
	 * @param request 
	 * @param customerName 
	 * @param request
	 * @return
	 */
	public List<Map<String, Object>> toCustomerList(int currentPage, int pageSize, String customerName, HttpServletRequest request) {
		/*分页整理*/
		int start = (currentPage-1) * pageSize;
		int end = pageSize;
		/*查询条件整理*/
		Map<String, Object> param = new HashMap<>();
		param.put("type", 2);
		param.put("deleteFlag", "0");
		param.put("customerName", customerName);
		param.put("tEid", AppUserHelper.getCurrentCompanyId(request));
		param.put("start",start);
		param.put("end", end);
		
		List<Map<String, Object>> customList = tCustomerMapper2.queryCustomerList(param);
		return customList;
	}
	
	/**
	 * 编辑客户
	 * @param tCustomer
	 */
	public Object editCustomer(TCustomer2 tCustomer2, MultipartFile[] files, String[] oldFile, HttpServletRequest request){
		/*修改客户信息*/
		int num = tCustomerMapper2.updateByPrimaryKeySelective(tCustomer2);
		if(num > 0){
			tCustomer2.setCreatorId(AppUserHelper.getCurrentUserId(request));
			tCustomer2.setCreatorName(AppUserHelper.getCurrentUserName(request));
			tCustomer2.setDeptId(AppUserHelper.getCurrentDeptId(request));
			tCustomer2.setDeptName(AppUserHelper.getCurrentDeptName(request));
			tCustomer2.settEId(AppUserHelper.getCurrentCompanyId(request));
			//先把原来的删除
			TCustomerAtt2 customerAtt2 = new TCustomerAtt2();
			customerAtt2.setDeleteFlag("0");
			customerAtt2.setType("2");
			customerAtt2.settCustomerId(tCustomer2.getId());
			List<TCustomerAtt2> tCustomerAttList = tCustomerAttMapper2.select(customerAtt2);
			for(TCustomerAtt2 customerAtt : tCustomerAttList){
				customerAtt.setDeleteFlag("1");
				tCustomerAttMapper2.updateByPrimaryKeySelective(customerAtt);
			}
	        /*保存客户附件信息*/
			MultipartRequest mRequest=(MultipartRequest)request;
			Iterator<String> fileNames = mRequest.getFileNames();
			while(fileNames.hasNext()){
				MultipartFile file = mRequest.getFile(fileNames.next());
				Map<String,Object> urlMap = null;
                try {
                    urlMap = UploadFile.compressUploadedFile(file);
                } catch (Exception e) {
                    return AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
                }
               
                TCustomerAtt2 customerAtt = new TCustomerAtt2();
                customerAtt.setUrl(urlMap.get("fileName")+"");
                customerAtt.setTurl(urlMap.get("thumbnails")+"");
                customerAtt.setType("2");
                customerAtt.settCustomerId(tCustomer2.getId());
                customerAtt.setDeleteFlag("0");
                customerAtt.setCreateTime(new Date());
                customerAtt.setCreatorId(AppUserHelper.getCurrentUserId(request));
                customerAtt.setCreatorName(tCustomer2.getCreatorName());
                customerAtt.setDeptId(tCustomer2.getDeptId());
                customerAtt.setDeptName(tCustomer2.getDeptName());
                customerAtt.settEId(tCustomer2.gettEId());
                tCustomerAttMapper2.insertSelective(customerAtt);
			}
			//先把原来的删除
			TCustomerArea2 tCustomerArea = new TCustomerArea2();
			tCustomerArea.settCustomerId(tCustomer2.getId());
			tCustomerAreaMapper2.delete(tCustomerArea);
			/*保存客户与地区关联信息*/
			TCustomerArea2 tCustomerArea2 = new TCustomerArea2();
			tCustomerArea2.settAreaId(tCustomer2.getAreaId());
			tCustomerArea2.settCustomerId(tCustomer2.getId());
			tCustomerAreaMapper2.insertSelective(tCustomerArea2);
	        return AppUtil.returnMapJson("1", "编辑成功!", null);
		} else {
			return AppUtil.returnMapJson("1", "编辑失败!", null);
		}
	}

	/**
	 * 保存客户信息
	 * @param customer
	 * @return 
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object addCustomer(TCustomer2 tCustomer2, MultipartFile[] files, HttpServletRequest request){
		
		/** 设置客户信息 */
		tCustomer2.setType("2");
		tCustomer2.setDeleteFlag("0");
		tCustomer2.setCreateTime(new Date());
		tCustomer2.setCreatorId(AppUserHelper.getCurrentUserId(request));
		tCustomer2.setCreatorName(AppUserHelper.getCurrentUserName(request));
		tCustomer2.setDeptId(AppUserHelper.getCurrentDeptId(request));
		tCustomer2.setDeptName(AppUserHelper.getCurrentDeptName(request));
		tCustomer2.settEId(AppUserHelper.getCurrentCompanyId(request));
		// 保存客户信息
		int num = tCustomerMapper2.insertSelective(tCustomer2);
		if(num > 0){
			Integer customerId = tCustomer2.getId();
			/*保存客户附件信息*/
			MultipartRequest mRequest=(MultipartRequest)request;
			Iterator<String> fileNames = mRequest.getFileNames();
			while(fileNames.hasNext()){
				MultipartFile file = mRequest.getFile(fileNames.next());
				Map<String,Object> urlMap = null;
                try {
                    urlMap = UploadFile.compressUploadedFile(file);
                } catch (Exception e) {
                    return AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
                }
               
                TCustomerAtt2 customerAtt = new TCustomerAtt2();
                customerAtt.setUrl(urlMap.get("fileName")+"");
                customerAtt.setTurl(urlMap.get("thumbnails")+"");
                customerAtt.setType("2");
                customerAtt.settCustomerId(customerId);
                customerAtt.setDeleteFlag("0");
                customerAtt.setCreateTime(new Date());
                customerAtt.setCreatorId(tCustomer2.getCreatorId());
                customerAtt.setCreatorName(tCustomer2.getCreatorName());
                customerAtt.setDeptId(tCustomer2.getDeptId());
                customerAtt.setDeptName(tCustomer2.getDeptName());
                customerAtt.settEId(tCustomer2.gettEId());
                tCustomerAttMapper2.insertSelective(customerAtt);
			}
			
			/*保存客户与地区关联信息*/
			TCustomerArea2 tCustomerArea2 = new TCustomerArea2();
			tCustomerArea2.settAreaId(tCustomer2.getAreaId());
			tCustomerArea2.settCustomerId(customerId);
			tCustomerAreaMapper2.insertSelective(tCustomerArea2);
			return AppUtil.returnMapJson("1", "保存成功!", null);
		} else {
			return AppUtil.returnMapJson("0", "保存失败!", null);
		}
	}
}
